﻿using System;
using System.Data.SqlClient;
using FTools.Dominio;
using System.Data;
using System.Collections;

namespace FTools.Persistencia
{
    class PerFabricante
    {
        static string msg = "";

        //OBTENER FABRICANTE
        public Fabricante obtenerFabricante(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdFabricante)
        {
            DataTable dataTable = new DataTable();
            Fabricante objFabricante = new Fabricante();
            try
            {
                SqlCommand command = new SqlCommand("select * from FABRICANTE where ID_FABRICANTE=@IdFabricante");

                command.Parameters.AddWithValue("@IdFabricante", xIdFabricante);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];

                    objFabricante.IdFabricante = (long)fila.ItemArray[0];
                    objFabricante.Nombre = (String)fila.ItemArray[1]; objFabricante.Nombre = objFabricante.Nombre.Trim();
                    objFabricante.Nivel = (int)fila.ItemArray[2];
                }
                return objFabricante;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE FABRICANTE
        public Boolean existeFabricante(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdFabricante)
        {
            DataTable dataTable = new DataTable();
            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from FABRICANTE where ID_FABRICANTE=@IdFabricante");

                command.Parameters.AddWithValue("@IdFabricante", xIdFabricante);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE FABRICANTE
        public Boolean existeFabricante(SqlConnection xConexion, SqlTransaction xTransaccion, String xNombre)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from FABRICANTE where NOMBRE=@Nombre");

                command.Parameters.AddWithValue("@Nombre", xNombre.Trim());
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //OBTENER COLECCION FABRICANTE
        public ArrayList obtenerColeccionFabricante(SqlConnection xConexion, SqlTransaction xTransaccion)
        {
            DataTable dataTable = new DataTable();
            SqlDataAdapter dataAdapter;
            ArrayList colFabricante = new ArrayList();

            try
            {
                SqlCommand command = new SqlCommand("select * from FABRICANTE");

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Fabricante objFabricante = new Fabricante();
                    DataRow fila = dataTable.Rows[i];
                    objFabricante.IdFabricante = (long)fila.ItemArray[0];
                    objFabricante.Nombre = (String)fila.ItemArray[1]; objFabricante.Nombre = objFabricante.Nombre.Trim();
                    objFabricante.Nivel = (int)fila.ItemArray[2];
                    colFabricante.Add(objFabricante);
                }
                return colFabricante;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //ALTA FABRICANTE
        public int altaFabricante(SqlConnection xConexion, SqlTransaction xTransaccion, Fabricante xObjFabricante)
        {
            try
            {
                SqlCommand command = new SqlCommand("insert into FABRICANTE values (@IdFabricante, @Nombre, @Nivel)");

                command.Parameters.AddWithValue("@IdFabricante", xObjFabricante.IdFabricante);
                command.Parameters.AddWithValue("@Nombre", xObjFabricante.Nombre.Trim());
                command.Parameters.AddWithValue("@Nivel", xObjFabricante.Nivel);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA FABRICANTE
        public int bajaFabricante(SqlConnection xConexion, SqlTransaction xTransaccion, Fabricante xObjFabricante)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from FABRICANTE where ID_FABRICANTE=@IdFabricante");

                command.Parameters.AddWithValue("@IdFabricante", xObjFabricante.IdFabricante);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //MODIFICA FABRICANTE
        public int modificaFabricante(SqlConnection xConexion, SqlTransaction xTransaccion, Fabricante xObjFabricante)
        {
            try
            {
                SqlCommand command = new SqlCommand("update FABRICANTE set NOMBRE=@Nombre, NIVEL=@Nivel " +
                                                    "where ID_FABRICANTE = @IdFabricante");

                command.Parameters.AddWithValue("@IdFabricante", xObjFabricante.IdFabricante);
                command.Parameters.AddWithValue("@Nombre", xObjFabricante.Nombre.Trim());
                command.Parameters.AddWithValue("@Nivel", xObjFabricante.Nivel);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }
    }
}
